package shoppingmall.reception.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shoppingmall.reception.mapper.MemberMapper;
import shoppingmall.reception.pojo.User;
import shoppingmall.reception.service.MemberService;

/**
 * 平台会员用户信息服务层接口实现
 */
@Service
public class MemberServiceImpl implements MemberService {

    //mapper层实例
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 查询平台会员信息(会员登录后获取信息，根据用户名或者用户手机)
     */
    @Override
    public User getMemberInfo(String usernameOrPhone) {
        //调用mapper层查询会员信息
        User userInfo = memberMapper.getUserInfoByUserName(usernameOrPhone);
        if(userInfo == null) {
            userInfo = memberMapper.getUserInfoByUserPhone(usernameOrPhone);
        }
        return userInfo;
    }

    /**
     * 修改平台会员信息
     */
    @Override
    public boolean updateMemberInfo(User user) {
        //调用mapper层方法
        try {
            int i = memberMapper.updateUserInfo(user);
            if(i == 0){
                //影响数据条数为0,返回false
                return false;
            }
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 插入一条平台会员用户信息
     */
    @Override
    public boolean saveMember(User user) {
        //调用mapper层方法
        try {
            memberMapper.saveUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 删除一条平台会员用户信息
     */
    @Override
    public boolean deleteMember(String username) {
        //调用mapper层方法
        try {
            memberMapper.deleteUser(username);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
